package ui;

import entity.TypeChar;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

public class TypePanel extends Panel implements Runnable, KeyListener {
    ArrayList<TypeChar> list = new ArrayList<TypeChar>();
    private static final int size = 20;
    private static final int COUNTDOWN_TIME = 60; // 倒计时时间，单位：秒
    private int remainingTime = COUNTDOWN_TIME;
    private boolean gameOver = false;
    private int score = 0; // 新增：分数

    public TypePanel() {
        // 初始化字符列表
        for (int i = 0; i < size; i++) {
            char value = createChars();
            int x = generateX();
            int y = 20;
            list.add(new TypeChar(value, x, y));
        }

        // 创建并启动线程
        Thread thread = new Thread(this);
        thread.start();

        // 创建并启动倒计时线程
        Thread countdownThread = new Thread(() -> {
            while (remainingTime > 0) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                remainingTime--;
            }
            gameOver = true;
        });
        countdownThread.start();
    }

    private int generateX() {
        int x;
        boolean overlap;
        do {
            x = (int) (Math.random() * TypeFrame.WIN_WIDTH + 10);
            overlap = false;
            for (TypeChar typeChar : list) {
                if (Math.abs(typeChar.getX() - x) < 10) {
                    overlap = true;
                    break;
                }
            }
        } while (overlap);
        return x;
    }

    private char createChars() {
        return (char) (Math.random() * 26 + 97); // a-z
    }

    @Override
    public void paint(Graphics g) {
        g.setFont(new Font("Arial", Font.BOLD, 20));
        if (gameOver) {
            g.setColor(Color.RED);
            g.setFont(new Font("Arial", Font.BOLD, 50));
            g.drawString("Game Over", TypeFrame.WIN_WIDTH / 2 - 100, TypeFrame.WIN_HEIGHT / 2);
            g.setFont(new Font("Arial", Font.BOLD, 20));
            g.drawString("Score: " + score, TypeFrame.WIN_WIDTH / 2 - 50, TypeFrame.WIN_HEIGHT / 2 + 50);
        } else {
            for (TypeChar typeChar : list) {
                g.drawString(Character.toString(typeChar.getValue()), typeChar.getX(), typeChar.getY());
            }
            g.setColor(Color.BLUE);
            g.drawString("Time: " + remainingTime + "s", 10, 30);
            g.drawString("Score: " + score, 10, 60);
        }
    }

    @Override
    public void run() {
        while (!gameOver) {
            for (TypeChar typeChar : list) {
                if (typeChar.getY() > TypeFrame.WIN_HEIGHT - 50) {
                    typeChar.setY(10);
                    typeChar.setX(generateX());
                    typeChar.setValue(createChars());
                } else {
                    typeChar.setY(typeChar.getY() + 1 + (int) (Math.random() * 5 + 1));
                }
            }
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            repaint(); // 触发重绘
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (!gameOver) {
            for (TypeChar typeChar : list) {
                if (typeChar.getValue() == e.getKeyChar()) {
                    typeChar.setValue(createChars());
                    typeChar.setX(generateX());
                    typeChar.setY(10);
                    score++; // 新增：每次正确输入字符，分数加 1
                    repaint(); // 重新绘制界面以更新分数显示
                }
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}